#include <dht.h>
#include <math.h>
const float R=10000.0;
dht DHT;

#define DHT11_PIN 8  //put the sensor in the digital pin 2 
#define DHT11_PIN_WHITE 2
#define DHT11_PIN_GREEN 3
#define DHT11_PIN_RED 4
void setup(){
  Serial.begin(9600);
  pinMode(DHT11_PIN,OUTPUT);
  pinMode(DHT11_PIN_WHITE,OUTPUT);
  pinMode(DHT11_PIN_GREEN,OUTPUT);
  pinMode(DHT11_PIN_RED,OUTPUT);
  digitalWrite(DHT11_PIN_WHITE,LOW);
  digitalWrite(DHT11_PIN_GREEN,LOW);
  digitalWrite(DHT11_PIN_RED,LOW);
  Serial.println("DHT TEST PROGRAM ");  
  Serial.print("LIBRARY VERSION: ");  
  Serial.println(DHT_LIB_VERSION);  
  Serial.println();  
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)"); 
}

void loop(){
  //READ DATA
  Serial.print("DHT11,\t");
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: 
                Serial.println("OK,\t"); 
                break;
    case -1: 
                Serial.println("Checksum error,\t"); 
                break;
    case -2: 
                Serial.println("Time out error,\t"); 
                break;
    default: 
                Serial.println("Unknown error,\t"); 
                break;
  }
  
  float AI,Rt1,temp;
  int value = analogRead( A0 );
  AI=(5/1023.0)*value;
  Rt1=( AI*R )/( 5-AI );
  temp=3950*(273.15+25)/(3950+(273.15+25)*log(Rt1/10000)); 
  Serial.print("Rt_value = ");
  Serial.print(Rt1);
  Serial.print("; Voltage_value = ");
  Serial.print(AI);
  Serial.print("; Digital_value = ");
  Serial.print(value);
  Serial.print("; Temp_value = ");
  Serial.println(temp);
  
  //DISPLAY DATA
  Serial.print("Humidity (%): ");
  Serial.print(DHT.humidity,2);  
  Serial.print(",\t");  
  Serial.print("Temperature (oC): ");
  Serial.println(DHT.temperature,2);
  /*
  Serial.println((float)DHT11.humidity, 2);
  Serial.println((float)DHT11.temperature, 2);*/
  if(DHT.humidity>=100&&DHT.temperature<29)
  {
    digitalWrite(DHT11_PIN_WHITE,HIGH);
    digitalWrite(DHT11_PIN_GREEN,LOW);
    digitalWrite(DHT11_PIN_RED,LOW);
  }
  else if(DHT.humidity<100&&DHT.temperature>=29)
  {
    digitalWrite(DHT11_PIN_WHITE,LOW);
    digitalWrite(DHT11_PIN_GREEN,HIGH);
    digitalWrite(DHT11_PIN_RED,LOW);
  }
  else if(DHT.humidity>=100&&DHT.temperature>=29)
  {
    digitalWrite(DHT11_PIN_WHITE,LOW);
    digitalWrite(DHT11_PIN_GREEN,LOW);
    digitalWrite(DHT11_PIN_RED,HIGH);
  }
  else
  {
    digitalWrite(DHT11_PIN_WHITE,LOW);
    digitalWrite(DHT11_PIN_GREEN,LOW);
    digitalWrite(DHT11_PIN_RED,LOW);
  } 
  
  delay(1000);
}
